//
//  RootNavController.m
//  BeautyBaoUnion
//
//  Created by 肖智 on 2016/10/20.
//  Copyright © 2016年 肖智. All rights reserved.
//

#import "RootNavController.h"
#import "MyTabBarController.h"

@interface RootNavController (){
}
@property(nonatomic,assign)BOOL hasAppear;
@property(nonatomic,strong)UILabel *lbl;
@property(nonatomic,strong)UIButton *enterbutton;
@property(nonatomic,assign)NSInteger count;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,strong)UIImageView *bgImg;
@property(nonatomic,strong)UIImageView *adImg;
@property(nonatomic,strong)UIView *defaultView;
@end

@implementation RootNavController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarHidden = YES;
    [UIApplication sharedApplication].statusBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    _count = 3;
    _bgImg = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _bgImg.image = [UIImage imageNamed:@"yindao"];
    
    _adImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height*0.8)];
    [self.view addSubview:_bgImg];
    [self.view addSubview:_adImg];
    
    _bgImg.image = [UIImage imageNamed:@"yindao"];
    
    [self performSelector:@selector(beginCount) withObject:nil afterDelay:1.5];
    
    // Do any additional setup after loading the view.
}
- (void)beginCount{
    [self.view addSubview:self.defaultView];
    [self.view addSubview:self.lbl];
    [self.view addSubview:self.enterbutton];
    self.enterbutton.center = self.lbl.center;
    [self.view bringSubviewToFront:self.lbl];
    NSString *fix = @"跳过(3)";
    _lbl.attributedText = [fix genAttributStringWithSpecialStrs:@[@"3"] commonColor:self.lbl.textColor specialColor:[UIColor redColor] commonFont:self.lbl.font specialFont:[UIFont systemFontOfSize:14] textAlignment:NSTextAlignmentCenter lineSpace:0];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dealWithTimer:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
}
- (void)next{
    [_timer invalidate];
    self.timer = nil;
    [self.defaultView removeFromSuperview];
    [self.lbl removeFromSuperview];
    [self.enterbutton removeFromSuperview];
    [_bgImg removeFromSuperview];
    [_adImg removeFromSuperview];
    MyTabBarController *tabCtl = [[MyTabBarController alloc] init];
    [self pushViewController:tabCtl animated:YES];
}
- (void)dealWithTimer:(NSTimer *)timer{
    _count--;
    if(_count<0){
        [_timer invalidate];
        self.timer = nil;
        [self.defaultView removeFromSuperview];
        [self.lbl removeFromSuperview];
        [self.enterbutton removeFromSuperview];
        [_bgImg removeFromSuperview];
        [_adImg removeFromSuperview];
        MyTabBarController *tabCtl = [[MyTabBarController alloc] init];
        [self pushViewController:tabCtl animated:YES];
        
    }else{
        NSString *text2 = [NSString stringWithFormat:@"跳过(%ld)",_count];
        NSString *fix = [NSString stringWithFormat:@"%ld",_count];
        _lbl.attributedText = [text2 genAttributStringWithSpecialStrs:@[fix] commonColor:self.lbl.textColor specialColor:[UIColor redColor] commonFont:self.lbl.font specialFont:[UIFont systemFontOfSize:14] textAlignment:NSTextAlignmentCenter lineSpace:0];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)prefersStatusBarHidden{
    return YES;
}
-(NSString*)weekdayStringFromDate:(NSDate*)inputDate {
    
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期天", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
    
}
- (NSString *)genStringFromDate:(NSDate *)date{
    NSString *str = [self weekdayStringFromDate:date];
    NSDateFormatter *dateForamt = [[NSDateFormatter alloc] init];
    dateForamt.dateFormat = @"yyyy 年 MM 月 dd 日  ";
    NSString *dateStr = [dateForamt stringFromDate:date];
    return [NSString stringWithFormat:@"%@%@",dateStr,str];
}

#pragma mark lazy method
- (UIView *)defaultView{
    if(!_defaultView){
        _defaultView = [[UIView alloc] initWithFrame:self.adImg.bounds];
        //图片
        UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 87, 92)];
        imgV.image = [UIImage imageNamed:@"face"];
        imgV.center = CGPointMake(_defaultView.bounds.size.width/2.0, _defaultView.bounds.size.height/2.0);
        [_defaultView addSubview:imgV];
        //文字
        NSDate *date = [NSDate date];
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imgV.frame)+30, self.view.bounds.size.width, 30)];
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.textColor = [UIColor darkGrayColor];
        lbl.font = [UIFont systemFontOfSize:14.0];
        lbl.text = [self genStringFromDate:date];
        [_defaultView addSubview:lbl];
        
        
        lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(lbl.frame)+5, self.view.bounds.size.width, 20)];
        lbl.textAlignment = NSTextAlignmentCenter;
        lbl.textColor = [UIColor darkGrayColor];
        lbl.font = [UIFont systemFontOfSize:14.0];
        lbl.text = @"很高兴遇见你";
        [_defaultView addSubview:lbl];
    }
    return _defaultView;
}
- (UIButton *)enterbutton{
    if(!_enterbutton){
        _enterbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        _enterbutton.frame = CGRectMake(0,0,80,30);
        _enterbutton.backgroundColor =[[UIColor blackColor] colorWithAlphaComponent:0.4];
        _enterbutton.layer.cornerRadius = 4.0;
        [_enterbutton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    }
    return _enterbutton;
}
- (UILabel *)lbl{
    if(!_lbl){
        _lbl = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width-85,20,50, 16)];
        _lbl.textAlignment = NSTextAlignmentCenter;
        _lbl.textColor = [UIColor whiteColor];
        _lbl.font = [UIFont systemFontOfSize:14.0];
    }
    return _lbl;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
